package com.zmcn.fileTransfer.business.filetransfer.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.web.bind.ServletRequestUtils;

import com.zmcn.fileTransfer.fw.action.BaseAction;
import com.zmcn.fileTransfer.fw.util.CodeUtil;
import com.zmcn.fileTransfer.fw.util.HttpNet;
import com.zmcn.fileTransfer.fw.util.OutUtil;

@Action(value = "/center")
public class FileCenterAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -9118813145878510423L;
	private static final Log log = LogFactory.getLog(FileCenterAction.class);

	/**
	 * 获取文件信息
	 * 
	 * @return
	 */
	public String getFileInfo() {
		try {
			HttpServletRequest req = getRequest();
			String token = ServletRequestUtils.getStringParameter(req, "token");
			String souUrl = ServletRequestUtils.getStringParameter(req, "souUrl");
			//
			String url = souUrl + "/sou!getFileInfo.action";
			Map<String, Object> para = new HashMap<String, Object>();
			para.put("token", token);
			byte[] b = new HttpNet().getBytesFromNet(url, para);
			OutUtil.out(b, getResponse());
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return null;
	}

	/**
	 * 读取文件特定切片的字节流
	 * 
	 * @return
	 */
	public String readFile() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String fileName = ServletRequestUtils.getStringParameter(req, "fileName");
			int batch = ServletRequestUtils.getIntParameter(req, "batch");
			int size = ServletRequestUtils.getIntParameter(req, "size");
			String token = ServletRequestUtils.getStringParameter(req, "token");
			String souUrl = ServletRequestUtils.getStringParameter(req, "souUrl");
			//
			String url = souUrl + "/sou!readFile.action";
			Map<String, Object> para = new HashMap<String, Object>();
			para.put("fileName", fileName);
			para.put("batch", batch);
			para.put("size", size);
			para.put("token", token);
			byte[] b = new HttpNet().getBytesFromNet(url, para);
			OutUtil.out(b, getResponse());
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		return null;
	}

}
